$(function () {

    // 登录 注册 退出
    const token = localStorage.getItem('token')
    const username = localStorage.getItem('username')
    $('.username').html(username)
    if (token) {
        $('.notlogin').hide()
        $('.logined').show()
    } else {
        $('.notlogin').show()
        $('.logined').hide()
    }

    $('.fa-sign-out').on('click', function () {
        localStorage.removeItem('token')
        localStorage.removeItem('username')
        location.href = '/web-static/index.html'
    })


    function initcol() {
         axios.get('/user/userDataList?page=1&limit=10&type=1',{
            headers: { token }
    
        }).then(({data:res}) => {
            if (res.errno !== 0) {
                
                return console.log('数据请求失败');
            }
            console.log('数据请求成功');

            console.log(res);
            let htmlStr = template('list-box', res.data)
            $('#list_box').html(htmlStr)
        })

      
    }
    initcol()

    // 给收藏绑定点击事件
    $('body').on('click', '.btn-danger', function () {
        const id = $(this).attr('data-id')
        console.log(id);

       
        $.ajax({
            type: 'GET',
            url: 'http://124.223.14.236:8060/home/user/userDataHandle?type=1&action=del&article_id=' + id,
            headers: {
                token
            },
            success: function (res) {

                initcol()
            }
        })
    })


    axios.get('/index/index',{
        headers: { token }

    }).then(({data:res}) => {
        if (res.errno !== 0) {
            return console.log('数据请求失败');
        }
        //渲染首页表头列表   
        // let htmlStr1 = template('tpl-nav', res)
        // $('#index-nav').html(htmlStr1)

    })
   
    axios.get('/user/getUserInfo',
    {
        headers: { token }

    }).then(({ data: res }) => {
        console.log(res);
        if (res.errno !== 0) {
             layer.msg('获取用户信息失败')
            location.href = '/web-static/login.html'
        }
    })

})